function wxpay(app, amountTotal, orderId, redirectUrl) {
  let remark = "在线支付";
  let nextAction = {};
  if (orderId != 0) {
    remark = "支付订单 ：" + orderId;
    nextAction = { type: 0, id: orderId };
  }
  let school_id = wx.getStorageSync('school_id')
  wx.request({
    // 微信小程序支付调用统一下单
    url: app.globalData.api + '/api/v1.0/payment/' + school_id,
    data: {
      token: wx.getStorageSync('token'),
      order_id: orderId,
      amount_total: amountTotal,
      remark: remark,
      payName:"在线支付",
      nextAction: nextAction
    },
    method:'POST',
    success: function(res){
      if(res.data.code == 0){
        // 统一下单成功唤起微信支付窗口
        wx.requestPayment({
          timeStamp:res.data.data.timeStamp,
          nonceStr:res.data.data.nonceStr,
          package:'prepay_id=' + res.data.data.prepayId,
          signType:'MD5',
          paySign:res.data.data.sign,
          success:function () {
            wx.showToast({title: '支付成功'});
            wx.redirectTo({
              url: redirectUrl
            });
          },
          fail:function (err) {
            wx.showToast({title: '支付失败。' + err});
          }
        })
      } else {
        wx.showToast({ title: '统一下单失败。[' + res.data.code + ']' + res.data.msg})
      }
    }
  })
}

module.exports = {
  wxpay: wxpay
}
